package com.hyd.project;

import com.hyd.application.MyApplication;
import com.hyd.service.MusicService;
import com.hyd.view.RippleView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

/**
 * 关于我们界面，主体界面是h5
 * 
 * @author llf
 * 
 */
public class AboutUsActivity extends Activity {
	private WebView webView;
	private RelativeLayout parent;
	public static AboutUsActivity instance = null;

	// 播放器相关
	private RelativeLayout player_view;
	private ProgressBar progress;
	private Handler handler;
	private Runnable runnable;
	private ImageView play, loop;
	private RippleView playRipple, loopRipple, nextRipple, priorRipple;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about_us);

		initView();
		initClick();
	}

	private void initView() {
		instance = this;
		webView = (WebView) findViewById(R.id.about_us_webview);
		parent = (RelativeLayout) findViewById(R.id.parent);
		WebSettings settings = webView.getSettings();
		// 设置webview可以执行js代码
		settings.setJavaScriptEnabled(true);
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);
		// 设置web视图客户端
		webView.setWebViewClient(new MyWebViewClient());
		instance.webView
				.loadUrl("http://www.apkbus.com/android-126321-1-1.html");

		// 播放器相关
		player_view = (RelativeLayout) findViewById(R.id.player_view);
		play = (ImageView) player_view.findViewById(R.id.play);
		loop = (ImageView) player_view.findViewById(R.id.loop);
		playRipple = (RippleView) player_view.findViewById(R.id.play_ripple);
		loopRipple = (RippleView) player_view.findViewById(R.id.loop_ripple);
		nextRipple = (RippleView) player_view.findViewById(R.id.next_ripple);
		priorRipple = (RippleView) player_view.findViewById(R.id.prior_ripple);
		progress = (ProgressBar) player_view.findViewById(R.id.seek);

		handler = new Handler();
		runnable = new Runnable() {

			@Override
			public void run() {
				if (MyApplication.playstatus == 1) {
					progress.setMax(MusicService.player.getDuration());
					progress.setProgress(MusicService.player
							.getCurrentPosition());
				}
				// 延迟启动线程，主要是为了保证启动线程的时候,播放音乐的服务已经启动
				handler.postDelayed(runnable, 50);
			}
		};

		if (MusicService.player != null && MyApplication.playstatus == 1) {
			play.setBackgroundResource(R.drawable.player_hesitation);
			handler.post(runnable);
		} else if (MusicService.player != null && MyApplication.playstatus == 0) {
			progress.setMax(MusicService.player.getDuration());
			progress.setProgress(MusicService.player.getCurrentPosition());
		}
	}

	private void initClick() {
		playRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.playstatus == 0) {
					play.setBackgroundResource(R.drawable.player_hesitation);
					MyApplication.playstatus = 1;
					MusicService.goon();
					handler.post(runnable);
				} else {
					MusicService.pause();
					MyApplication.playstatus = 0;
					play.setBackgroundResource(R.drawable.play);

				}
			}
		});
		// 设置为循环播放
		loopRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.isLoop) {
					loop.setBackgroundResource(R.drawable.unloop);
					MusicService.player.setLooping(false);
					MyApplication.isLoop = false;
				} else {
					loop.setBackgroundResource(R.drawable.loop);
					MusicService.player.setLooping(true);
					MyApplication.isLoop = true;
				}
			}
		});

		nextRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playNext();
			}
		});

		priorRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playPrior();
			}
		});
	}

	public void back(View v) {
		finish();
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			webView.goBack();
			finish();
			overridePendingTransition(android.R.anim.slide_in_left,
					android.R.anim.slide_out_right);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		handler.removeCallbacks(runnable);
		parent.removeView(webView);
		webView.clearHistory();
		webView.removeAllViews();
		webView.destroy();
		super.onDestroy();
	}

	// webView的客户端
	public class MyWebViewClient extends WebViewClient {
		public boolean shouldOverviewUrlLoading(WebView view, String url) {
			return false;
		}
	}
}
